﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace XMM.Objects
{
    public class DVDPlaylist
    {
        private string _DriveLetter;
        public string DriveLetter { get { return _DriveLetter; } }

        private string _Path;
        public string Path { get { return _Path; } }

        private string _Title;
        public string Title { get { return _Title; } }

        public DVDPlaylist()
        {
            _DriveLetter = "D";
        }

        public DVDPlaylist(string sPath, string sDriveLetter, string sTitle)
        {
            _DriveLetter = sDriveLetter;
            _Path = sPath;
            _Title = sTitle;
        }

        public bool Save()
        {
            XmlDocument xdPlaylist = new XmlDocument();

            XmlNode xnDocumentNode = xdPlaylist.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlNamespaceManager oNamespaceManager = new XmlNamespaceManager(new NameTable());
            oNamespaceManager.AddNamespace("vlc", "http://www.videolan.org/vlc/playlist/ns/0/");

            xdPlaylist.AppendChild(xnDocumentNode);

            XmlElement xePlaylist = xdPlaylist.CreateElement("playlist");
            XmlAttribute xaPlaylistVersion = xdPlaylist.CreateAttribute("version");
            xaPlaylistVersion.InnerText = "1";
            XmlAttribute xaPlaylistXMLns = xdPlaylist.CreateAttribute("xmlns");
            xaPlaylistXMLns.InnerText = "http://xspf.org/ns/0/";
            XmlAttribute xaPlayListXMLnsVLC = xdPlaylist.CreateAttribute("xmlns:vlc");
            xaPlayListXMLnsVLC.InnerText = "http://www.videolan.org/vlc/playlist/ns/0/";
            xePlaylist.Attributes.Append(xaPlaylistVersion);
            xePlaylist.Attributes.Append(xaPlaylistXMLns);
            xePlaylist.Attributes.Append(xaPlayListXMLnsVLC);
            xdPlaylist.AppendChild(xePlaylist);

                XmlElement xeTitle = xdPlaylist.CreateElement("title");
                xeTitle.InnerText = "XMM Wiedergabeliste - " + Title;
                xePlaylist.AppendChild(xeTitle);

                XmlElement xeTrackList = xdPlaylist.CreateElement("trackList");
                xePlaylist.AppendChild(xeTrackList);

                    XmlElement xeTrack = xdPlaylist.CreateElement("track");
                    xeTrackList.AppendChild(xeTrack);

                    XmlElement xeLocation = xdPlaylist.CreateElement("location");
                    xeLocation.InnerText = "dvd://" + DriveLetter + ":\\";
                    xeTrack.AppendChild(xeLocation);

                    XmlElement xeExtension = xdPlaylist.CreateElement("extension");
                    XmlAttribute xaApplication = xdPlaylist.CreateAttribute("application");
                    xaApplication.InnerText = "http://www.videolan.org/vlc/playlist/0";
                    //xeExtension.Attributes.Append(xaApplication);
                    xeTrack.AppendChild(xeExtension);

                        //XmlNode xnVLCid = xdPlaylist.CreateElement("id", "vlc");
                        //XmlElement xeVLCid = xdPlaylist.CreateElement("vlc:id");
                        XmlElement xeVLCid = xdPlaylist.CreateElement("vlc", "id", "http://www.videolan.org/vlc/playlist/ns/0/");
                        xeVLCid.InnerText = "0";
                        xeExtension.AppendChild(xeVLCid);
                    
                    xeExtension.Attributes.Append(xaApplication);

                XmlElement xeExtensionData = xdPlaylist.CreateElement("extension");
            
                XmlAttribute xaExtensionApplication = xdPlaylist.CreateAttribute("application");
                xaExtensionApplication.InnerText = "http://www.videolan.org/vlc/playlist/0";
                xeExtensionData.Attributes.Append(xaExtensionApplication);
                xePlaylist.AppendChild(xeExtensionData);

                    //XmlElement xeVLCitem = xdPlaylist.CreateElement("vlc:item");
                    XmlElement xeVLCitem = xdPlaylist.CreateElement("vlc", "item", "http://www.videolan.org/vlc/playlist/ns/0/");
                    XmlAttribute xaTid = xdPlaylist.CreateAttribute("tid");
                    xaTid.InnerText = "0";
                    xeVLCitem.Attributes.Append(xaTid);
                    xeExtensionData.AppendChild(xeVLCitem);                    

            try
            {
                xdPlaylist.Save(Path);
                return true;
            }
            catch (Exception)
            {
                return false;
            }



            return false;
        }
    }
}
